package com.dudu.dao;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.mortbay.log.Log;

import com.dudu.model.MeetInfo;

import core.CoreDao;
import core.db.DBConnectionUtil;

/**
 * 会议记录信息持久化操作
 * 
 * @author 危东
 * @date 2016-4-28
 */
public class MeetInfoDao {
    private static final Logger log = core.util.Logger.getLogger(MeetInfoDao.class.getSimpleName());

    /**
     * 新增会议记录信息
     * 
     * @param dao
     * @param bean
     * @return
     */
    public int insertMeetBusiness(CoreDao dao, String cust_account, String sessionId, String sms_signature, String callback_interface_type, String digit) {
	Connection con = null;
	int result = -1;
	try {
	    // 获取数据库连接
	    con = DBConnectionUtil.getDBConnection("emate1");
	    StringBuffer sql = new StringBuffer("INSERT INTO t_meet_business(sessionId,cust_account,sms_signature,callback_interface_type,digit,create_time) ");
	    sql.append(" VALUES(?,?,?,?,?,SYSDATE())");
	    // 输出sql
	    List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(), new Object[] { sessionId, cust_account, sms_signature, callback_interface_type, digit });

	    JSONObject obj = list.get(0);
	    result = obj.optInt("result");

	} catch (Exception ex) {
	    Log.warn("新增会议业务信息出错" + ex);
	    ex.printStackTrace();
	} finally {
	    DBConnectionUtil.close(null, null, con);
	}
	return result;
    }

    /**
     * 新增会议记录信息
     * 
     * @param dao
     * @param bean
     * @return
     */
    public int insertMeetIms(CoreDao dao, MeetInfo bean) {
	Connection con = null;
	int result = -1;
	try {
	    // 获取数据库连接
	    con = DBConnectionUtil.getDBConnection("emate1");
	    StringBuffer sql = new StringBuffer(
		    "INSERT INTO t_meet_info(app_key,cust_account,user_identify,periphery_code,caller,called,session_id,duration,create_time,STATUS,total_amount,charge_number,comments,ability_type,room_id,msg_id) ");
	    sql.append(" VALUES(?,?,?,?,?,?,?,?,SYSDATE(),?,?,?,?,?,?,?)");
	    // 输出sql
	    List<JSONObject> list = dao.serviceJsonSql(
		    con,
		    sql.toString(),
		    new Object[] { bean.getApp_key(), bean.getCust_account(), bean.getUser_identify(), bean.getPeriphery_code(), bean.getCaller(), bean.getCalled(),
			    bean.getSession_id(), bean.getDuration(), bean.getStatus(), bean.getTotal_amount(), bean.getChargeNumber(), bean.getComments(), bean.getAbility_type(),
			    bean.getRoom_id(), bean.getMsg_id() });

	    JSONObject obj = list.get(0);
	    result = obj.optInt("result");

	} catch (Exception ex) {
	    Log.warn("新增会议记录信息出错" + ex);
	    ex.printStackTrace();
	} finally {
	    DBConnectionUtil.close(null, null, con);
	}
	return result;
    }

    /**
     * 更新被叫人数量
     * 
     * @param dao
     * @param bean
     * @return
     */
    public int updateMeetImsPhoneAndAcount(CoreDao dao, MeetInfo bean) {
	Connection con = null;
	int result = -1;
	try {
	    con = DBConnectionUtil.getDBConnection("emate1");
	    StringBuffer sql = new StringBuffer("UPDATE t_meet_info SET called=?,total_amount=? WHERE session_id=?");

	    List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(), new Object[] { bean.getCalled(), bean.getTotal_amount(), bean.getSession_id() });

	    if (list != null && list.size() > 0) {
		JSONObject obj = list.get(0);
		result = obj.optInt("result");
	    }

	} catch (Exception ex) {
	    ex.printStackTrace();
	} finally {
	    DBConnectionUtil.close(null, null, con);
	}

	return result;

    }

    /**
     * 根据会议标识查询会议详情
     * 
     * @param dao
     * @param sessionId
     * @return
     */
    public MeetInfo queryMeetImsBySessionId(CoreDao dao, String sessionId) {
	Connection con = null;
	MeetInfo result = null;
	try {
	    con = DBConnectionUtil.getDBConnection("emate1");
	    StringBuffer sql = new StringBuffer(
		    "SELECT id,app_key,cust_account,user_identify,periphery_code,caller,called,session_id,duration,create_time,STATUS,total_amount,charge_number,comments,is_accepted,ability_type,room_id FROM t_meet_info WHERE session_id=");
	    sql.append(" ? ");
	    List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(), new Object[] { sessionId });
	    // 判断查询是否为空
	    if (list != null && list.size() > 0) {
		// list形式转换
		List<MeetInfo> items = covertToBean(list);
		result = items.get(0);
	    }

	} catch (Exception ex) {
	    ex.printStackTrace();
	} finally {
	    DBConnectionUtil.close(null, null, con);
	}
	return result;
    }

    /**
     * 将json格式的list转换成MeetInfo对象list
     * 
     * @param objs
     * @return
     */
    private List<MeetInfo> covertToBean(List<JSONObject> objs) {
	log.info("covertToBean 将json格式的list转换成MeetInfo对象list");
	List<MeetInfo> list = new ArrayList<MeetInfo>();
	for (int i = 0; i < objs.size(); i++) {
	    JSONObject json = objs.get(i);
	    MeetInfo bean = (MeetInfo) JSONObject.toBean(json, MeetInfo.class);
	    list.add(bean);
	}
	return list;
    }

    /**
     * 根据会议数据唯一标识，更新会议sessionId
     * 
     * @param dao
     * @param sessionId
     * @param meetId
     * @return
     */
    public int updateSessionByVoice(CoreDao dao, String sessionId, String meetId) {
	Connection con = null;
	int result = -1;
	try {
	    con = DBConnectionUtil.getDBConnection("emate1");
	    StringBuffer sql = new StringBuffer("UPDATE t_meet_info SET session_id=?,is_accepted=1 WHERE id=?");

	    List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(), new Object[] { sessionId, meetId });

	    if (list != null && list.size() > 0) {

		JSONObject obj = list.get(0);
		result = obj.optInt("result");

	    }
	} catch (Exception ex) {
	    ex.printStackTrace();
	} finally {
	    DBConnectionUtil.close(null, null, con);
	}

	return result;

    }

    /**
     * 
     * @param dao
     * @param status
     * @param sessionId
     * @return
     */
    public int updateRecordReceiveStatus(CoreDao dao, String status, String sessionId) {

	Connection con = null;
	int result = -1;
	try {
	    con = DBConnectionUtil.getDBConnection("emate1");
	    StringBuffer sql = new StringBuffer("UPDATE t_meet_info SET is_accepted=" + status);
	    sql.append(" where session_id='" + sessionId + "'");

	    List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(), new Object[] { null });

	    if (list != null && list.size() > 0) {
		list.get(0);
		JSONObject obj = list.get(0);
		result = obj.optInt("result");
	    }

	} catch (Exception ex) {
	    ex.printStackTrace();
	} finally {
	    DBConnectionUtil.close(null, null, con);
	}

	return result;
    }

    public int updateEcpComAmount(CoreDao dao, MeetInfo bean) {

	Connection con = null;
	int result = -1;
	try {
	    con = DBConnectionUtil.getDBConnection("emate1");
	    StringBuffer sql = new StringBuffer("UPDATE t_meet_info SET duration=(duration+?) ");
	    sql.append(" where session_id=?");

	    List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(), new Object[] { bean.getDuration(), bean.getSession_id() });

	    if (list != null && list.size() > 0) {
		list.get(0);
		JSONObject obj = list.get(0);
		result = obj.optInt("result");
	    }

	} catch (Exception ex) {
	    ex.printStackTrace();
	} finally {
	    DBConnectionUtil.close(null, null, con);
	}

	return result;
    }
}
